package com.bj58.heatmap.db;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.bj58.heatmap.model.HomeHeatMap;
import com.bj58.heatmap.model.HomeHeatMap_PC;
import com.google.common.collect.Lists;

public class HeatMapService {
	CommonDao commonDao = new CommonDao();
	
	public  int countAll(String stat_date){
		String tablename = "HomeHeatMap_"+stat_date.replace("-", "").substring(0,6);
		
		String sql = String.format("select count(1) from %s where stat_date='%s'", tablename,stat_date);
		try {
			int count = commonDao.countAll(sql, DBConfig.db_131_data_bi.getIntType());
			return count;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		
	}
	
	
	public  Map<String, HomeHeatMap> getAllData(String stat_date){
		String tablename = "HomeHeatMap_"+stat_date.replace("-", "").substring(0,6);
		String sql = String.format("select * from %s where stat_date='%s'", tablename,stat_date);

		Map<String, HomeHeatMap> rsMap = new HashMap<String, HomeHeatMap>();
		List<HomeHeatMap> rs;
		try {
			rs = commonDao.getAllColsByVo(sql, HomeHeatMap.class, DBConfig.db_131_data_bi.getIntType());
			for(HomeHeatMap vo: rs){
//				String keys[] ={vo.getStat_date(),vo.getPlatform(),vo.getCityPingyin(),vo.getClickID()};
				rsMap.put(vo.getClickID() , vo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return rsMap;
	}
	
	
	public Map<String, HomeHeatMap> getCityData(String stat_date,String cityPingyin,String platform){
		String tablename = "HomeHeatMap_"+stat_date.replace("-", "").substring(0,6);
		String sql = String.format("select * from %s where stat_date='%s' and cityPingyin='%s' and platform='%s'",tablename, stat_date,cityPingyin,platform);
		
		Map<String, HomeHeatMap> rsMap = new HashMap<String, HomeHeatMap>();
		List<HomeHeatMap> rs;
		try {
			rs = commonDao.getAllColsByVo(sql, HomeHeatMap.class, DBConfig.db_131_data_bi.getIntType());
			for(HomeHeatMap vo: rs){
//				String keys[] ={vo.getStat_date(),vo.getPlatform(),vo.getCityPingyin(),vo.getClickID()};
				rsMap.put(vo.getClickID() , vo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return rsMap;
	}
	
	public Map<String, HomeHeatMap_PC> getCityData_PC(String stat_date,String cityPingyin,String platform){
		String tablename = "HomeHeatMap_PC_"+stat_date.replace("-", "").substring(0,6);
		String sql = String.format("select * from %s where stat_date='%s' and cityPingyin='%s' and platform='%s'",tablename, stat_date,cityPingyin,platform);
		
		Map<String, HomeHeatMap_PC> rsMap = new HashMap<String, HomeHeatMap_PC>();
		List<HomeHeatMap_PC> rs;
		try {
			rs = commonDao.getAllColsByVo(sql, HomeHeatMap_PC.class, DBConfig.db_131_data_bi.getIntType());
			for(HomeHeatMap_PC vo: rs){
				String keys[] ={vo.getClickID(), vo.getClickName(), vo.getTongji_tag()};
				rsMap.put(StringUtils.join(keys, "_") , vo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return rsMap;
	}
	
	public Map<String, HomeHeatMap_PC> getCityData_PC_Month(String stat_date,String cityPingyin,String platform){
		String tablename = "HomeHeatMap_PC_"+stat_date.replace("-", "").substring(0,6);
		String sql = String.format("select clickID, clickName, tongji_tag, sum(click_pv)/count(1), sum(click_uv)/count(1), sum(all_pv)/count(1), sum(all_vppv)/count(1),sum(all_vpuv)/count(1) from %s where cityPingyin='%s' and platform='%s' group by clickID, clickName, tongji_tag",
						tablename, cityPingyin, platform);
		List<String> cols = Lists.newArrayList("clickID","clickName","tongji_tag","click_pv","click_uv","all_pv","all_vppv","all_vpuv");
		Map<String, HomeHeatMap_PC> rsMap = new HashMap<String, HomeHeatMap_PC>();
		List<HomeHeatMap_PC> rs;
		try {
			rs = commonDao.getCommonStatList(sql, cols, HomeHeatMap_PC.class, DBConfig.db_131_data_bi.getIntType());
			for(HomeHeatMap_PC vo: rs){
				String keys[] ={vo.getClickID(), vo.getClickName(), vo.getTongji_tag()};
				rsMap.put(StringUtils.join(keys, "_") , vo);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return rsMap;
	}
}
